package javatest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

public class DynamicProxyTest {

    public static void main(String[] args) {
        DynamicProxyInterface test = (DynamicProxyInterface) Proxy.newProxyInstance(
                DynamicProxyInterface.class.getClassLoader(), new Class[]{DynamicProxyInterface.class}, new StubServiceDelegate());
        Collection<String> string = test.draftWhiteList();
        System.out.println(string.size());
    }


    static class StubServiceDelegate implements InvocationHandler {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) {
            Class<?> returnType = method.getReturnType();

            if (returnType.isAssignableFrom(Void.class) || returnType.getName().equals("void")) {
                return null;
            } else if (returnType.isAssignableFrom(int.class) ||
                    returnType.isAssignableFrom(Integer.TYPE)) {
                return 0;
            } else if (returnType.isAssignableFrom(boolean.class) ||
                    returnType.getName().equals("java.lang.Boolean") ||
                    returnType.isAssignableFrom(boolean.class)) {
                return false;
            } else if (returnType.isAssignableFrom(String.class) ||
                    returnType.getName().equals("java.lang.String")) {
                return "";
            } else if (returnType.isAssignableFrom(float.class) ||
                    returnType.isAssignableFrom(java.lang.Float.TYPE) ||
                    returnType.getName().equals("java.lang.Float")) {
                return 0.0f;
            } else if (returnType.isAssignableFrom(long.class) ||
                    returnType.isAssignableFrom(java.lang.Long.TYPE) ||
                    returnType.getName().equals("java.lang.Long")) {
                return 0L;
            } else if (returnType.isInterface()) {
                return Proxy.newProxyInstance(
                        returnType.getClassLoader(), new Class[]{returnType}, this);
            }
            return null;
        }
    }
}
